package com.educenter.service.impl;

import com.JwtUtils;
import com.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.exceptionhandler.MyException;
import com.educenter.entity.UcenterMember;
import com.educenter.entity.vo.RegisterVo;
import com.educenter.mapper.UcenterMemberMapper;
import com.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-11
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

	@Autowired
	private RedisTemplate<String,String> redisTemplate;

	@Override
	public String login(UcenterMember member) {
		String mobile = member.getMobile();
		String password = member.getPassword();

		//手机号码和密码非空判断
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
			throw new MyException(20001,"手机号或密码为空");
		}

		QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
		wrapper.eq("mobile",mobile);
		UcenterMember mobileMember = baseMapper.selectOne(wrapper);

		//判断用户是否存在
		if (mobileMember == null){
			throw new MyException(20001,"没有这个手机号");
		}
		//判断用户密码
		if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
			throw new MyException(20001,"密码错误");
		}
		//判断用户是否被禁用
		if (mobileMember.getIsDisabled()){
			throw new MyException(20001,"用户已被禁用");
		}

		String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
		return jwtToken;
	}

	@Override
	public void register(RegisterVo registerVo) {
		//获取注册的数据
		String mobile = registerVo.getMobile();
		String nickname = registerVo.getNickname();
		String code = registerVo.getCode();
		String password = registerVo.getPassword();

		//非空判断
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
				|| StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)){
			throw new MyException(20001,"任意项为空");
		}

		//判断验证码，从redis中获取验证码
		String redisCode = redisTemplate.opsForValue().get(mobile);
		if (!redisCode.equals(code)){
			throw new MyException(20001,"验证码不正确");
		}

		//判断手机号是否重复,表中存在则无法添加
		QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
		wrapper.eq("mobile",mobile);
		Integer count = baseMapper.selectCount(wrapper);
		if (count > 0){
			throw new MyException(20001,"账号已存在");
		}

//		BeanUtils.copyProperties(ucenterMember,registerVo);

		UcenterMember ucenterMember = new UcenterMember();
		ucenterMember.setMobile(mobile);
		ucenterMember.setPassword(MD5.encrypt(password));
		ucenterMember.setNickname(nickname);
		ucenterMember.setIsDisabled(false);
		ucenterMember.setGmtCreate(new java.sql.Date(new Date().getTime()));
		ucenterMember.setGmtModified(new java.sql.Date(new Date().getTime()));
		ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");

		baseMapper.insert(ucenterMember);
	}

	@Override
	public Integer countRegisterDay(String day) {
		return baseMapper.countRegisterDay(day);
	}
}
